package com.houtai.service.article.cate;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.RetInfo;
import com.houtai.bean.TsArticleCate;
import com.houtai.bean.dto.article.comments.TsArticleCommentsListDto;
import com.houtai.bean.vo.article.comments.TsArticleCommentsList;
import com.houtai.bean.vo.article.comments.TsArticleCommentsReplyList;
import com.houtai.common.util.LogUtils;
import com.houtai.dao.TsArticleCateDao;
import com.houtai.dao.TsArticleCommentsDao;
import com.houtai.dao.TsJzAdminOperationDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2017/4/22.
 */
@Service
public class ArticleCateService {
    private static Logger logger = Logger.getLogger(ArticleCateService.class);
    @Autowired
    private TsArticleCateDao tsArticleCateDao;
    @Autowired
    private TsArticleCommentsDao tsArticleCommentsDao;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TsJzAdminOperationDao tsJzAdminOperationDao;


    public RetInfo insertArticleCate(TsArticleCate tsArticleCate , HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":insertArticleCate:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            tsArticleCateDao.insertTsArticleCate(tsArticleCate);
            retInfo.setMark("0");
            retInfo.setTip("成功");
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"添加文章类别 类别id：" + tsArticleCate.getCate_id() , request);
        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("出错了");
            logger.error("======error:" + e.toString() + "======");
            throw new RuntimeException();
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    public RetInfo updateArticleCate(TsArticleCate tsArticleCate , HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateArticleCate:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TsArticleCate articleCate = tsArticleCateDao.findTsArticleCateById(tsArticleCate.getCate_id());
            if (articleCate != null) {
                tsArticleCateDao.updateTsArticleCate(tsArticleCate);
                retInfo.setMark("0");
                retInfo.setTip("成功");
                LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"修改文章类别 类别id：" + tsArticleCate.getCate_id() , request);

            } else {
                retInfo.setMark("1");
                retInfo.setTip("文章类别不存在");
            }

        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("出错了");
            logger.error("======error:" + e.toString() + "======");
            throw new RuntimeException();
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }



    public List<TsArticleCate> findArticleCates() {
        String logInfo = this.getClass().getName() + ":findArticleCates:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        List<TsArticleCate> tsArticleCates = null;
        try {
            tsArticleCates = tsArticleCateDao.findTsArticleCatePage();
        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
        }
        logger.info("======" + logInfo + "end======");
        return tsArticleCates;
    }


    public TsArticleCate findArticleCateById(int cate_id) {
        return tsArticleCateDao.findTsArticleCateById(cate_id);
    }
    public RetInfo updateArticleCateState(int cate_id , HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateArticleCate:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TsArticleCate articleCate = tsArticleCateDao.findTsArticleCateById(cate_id);
            if (articleCate != null) {
                tsArticleCateDao.updateTsArticleCateValid(cate_id);
                retInfo.setMark("0");
                retInfo.setTip("成功");
                LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"删除文章类别 类别id：" + cate_id , request);

            } else {
                retInfo.setMark("1");
                retInfo.setTip("文章类别不存在");
            }

        } catch (Exception e) {
            retInfo.setMark("-1");
            retInfo.setTip("出错了");
            logger.error("======error:" + e.toString() + "======");
            throw new RuntimeException();
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    文章评论列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ArticleListVo
     */
    public RetInfo commentsList(HttpServletRequest request, TsArticleCommentsListDto dto) {
        String logInfo = this.getClass().getName() + ":commentsList:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {

            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), "comments_id DESC");
            List<TsArticleCommentsList> lists = tsArticleCommentsDao.findTsArticleCommentsList(dto);
            PageInfo<TsArticleCommentsList> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            if (list.getTotal() > 0) {
                for (int i = 0; i < list.getList().size(); i ++){
                    List<TsArticleCommentsReplyList> reply = tsArticleCommentsDao.findTsArticleCommentsReplyList(list.getList().get(i).getComments_id());
                    list.getList().get(i).setReplys_size(reply.size());
                    list.getList().get(i).setReplys(reply);
                }



            }
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("article_id", dto.getArticle_id());
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查看文章评论列表 文章id：" + dto.getArticle_id() , request);
        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
